package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	// _ "github.com/mattn/go-sqlite3"
	"log"
)

func RegisterDB() {
	orm.RegisterModel(new(User))
	// orm.RegisterDriver("sqlite", orm.DR_Sqlite)
	// orm.RegisterDataBase("default", "sqlite3", "/home/wayne/sqlite3test/test.db", 30)
	orm.RegisterDriver("mysql", orm.DR_MySQL)
	orm.RegisterDataBase("default", "mysql", "root:beatrice@/pms?charset=utf8", 30)
	MigrationDB()
}

func MigrationDB() {
	overwrite, _ := beego.AppConfig.Bool("database_overwrite")
	output, _ := beego.AppConfig.Bool("show_database_output")
	orm.RunSyncdb("default", overwrite, output)

	o := orm.NewOrm()
	o.Using("default")

	u1 := &User{0, "wayne", "1234"}
	id, err := o.Insert(u1)
	if err != nil {
		log.Println(err.Error())
	} else {
		log.Println("User added,ID is: ", id)
	}

}
